package edu.bucknell.gaugetest;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.widget.ImageView;

public class Gauge extends ImageView {
	private Bitmap backgroundIm;
	private Bitmap background;
	private Bitmap needle;
	private Handler h;
	private float angle = 0;
	private int scale;
	private float velocity;
	private float destination = 0;
	private float minAngle =0;
	private float maxAngle = 180;
	private float minVal = 0;
	private float maxVal = 100;

	//private float acceleration;

	private final static int FRAME_RATE = 30;
	private static final float TICKS_TO_MOVE =(float) FRAME_RATE;
	private static final float EPSILON = (float) 0.1;

	// To make it sleep for the right amount of time
	private Runnable r = new Runnable() {
		@Override
		public void run() {
			invalidate();
		}

	};
	/**
	 * Constructors that are inherited
	 */
	public Gauge(Context context) {
		super(context);
		h = new Handler();
	}

	public Gauge(Context context, AttributeSet attrs) {
		super(context, attrs);
		h = new Handler();
		init(attrs);
	}

	public Gauge(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		h = new Handler();
		init(attrs);
	}

	private void init(AttributeSet attrs) {
		System.out.println("ATTRIBUTE SET STUFF");
		System.out.println(attrs.getAttributeCount());

	}


	public void setBounds(float minAngle, float maxAngle, float minValue, float maxValue){
		this.minAngle = minAngle;
		this.maxAngle = maxAngle;
		this.minVal = minValue;
		this.maxVal = maxValue;
	}

	public void setTargetVal(float value){
		float angle = (value-minVal) *((maxAngle-minAngle)/(maxVal-minVal)) +minAngle;
		System.out.println("The target Angle is: "+angle);
		setNeedleDestination(angle);

	}

	public void setNeedleDestination(float angle){
		destination = angle;
		velocity = (destination-this.angle)/TICKS_TO_MOVE;
	}

	/**
	 * Updating the needle tilt angle
	 * @param angle
	 */
	private void setNeedleAngle(){
		if(!aproxEquals(destination, angle))
			this.angle += velocity;
	}


	/**
	 * Overridden on draw call. This call is made whenever the GUI is to be updated
	 */
	protected void onDraw(Canvas canvas){
		drawNeedle(canvas);
		h.postDelayed(r, FRAME_RATE);
	}

	/**
	 * Draw the needle on the screen at the correct angle
	 * @param tempCan
	 */
	private void drawNeedle(Canvas tempCan){
		setNeedleAngle();
		tempCan.scale((float)scale, (float)scale);
		tempCan.drawBitmap(backgroundIm, 0, 0, null);
		tempCan.rotate(angle,background.getWidth()/2,background.getHeight()/2);
		tempCan.drawBitmap(needle, 0, 0, null);
		tempCan.rotate(-angle,background.getWidth()/2,background.getHeight()/2);
	}

	public void setNeedle(Bitmap needle){
		this.needle = needle;


	}

	public void setBackground(Bitmap backgr){
		this.scale = 1;
		if(backgr.getHeight() == 200){
			this.scale = 2;
		}
		System.out.println(backgr.getWidth());
		this.backgroundIm = backgr;
		this.background =Bitmap.createBitmap(backgr.getWidth(), backgr.getHeight(), Bitmap.Config.ARGB_8888);

	}

	boolean aproxEquals(float f1, float f2){
		if (Math.abs(f1-f2)>EPSILON)
			return false;
		return true;
	}
}

